/**
 * 根据GPS位置获取地址信息
 */
function getAddress(callback) {
    var geolocation = new BMap.Geolocation();
    //逆向解析
    geolocation.getCurrentPosition(function (r) {
        if (this.getStatus() == BMAP_STATUS_SUCCESS) {
            var mk = new BMap.Marker(r.point);
            var geoc = new BMap.Geocoder();
            geoc.getLocation(r.point, function (rs) {
                var addComp = rs.addressComponents;
                //返回地址信息
                var address = addComp.city + addComp.district + addComp.street + addComp.streetNumber;
                callback(address);
            });
        }
        else {
            alert('failed' + this.getStatus());
        }
    }, {enableHighAccuracy: true})
}

/**
 * 获取所在位置坐标
 * @return json  {lng:'经度',lat:'纬度'}
 */
function getGeolocation(callback)
{
    var geolocation = new BMap.Geolocation();
    geolocation.getCurrentPosition(function (r) {
        if (this.getStatus() == BMAP_STATUS_SUCCESS) {
            callback(r.point);
        }
        else {
            alert('failed' + this.getStatus());
        }
    }, {enableHighAccuracy: true})
}

/**
 * 获取2点间的距离
 * @param pos  {lng1:经度,lat1:纬度,lng2:经度,lat2:纬度}
 */
function getDistance(pos)
{
    var map = new BMap.Map();
    var pointA = new BMap.Point(pos.lng1,pos.lat1);  // 创建点坐标A--大渡口区
    var pointB = new BMap.Point(pos.lng2,pos.lat2);  // 创建点坐标B--江北区
    return (map.getDistance(pointA,pointB)).toFixed(2);  //获取两点距离,保留小数点后两位
}

/**
 * 有搜索框与坐标表单的地图
 */
function showMap() {
    // 百度地图API功能
    function G(id) {
        return document.getElementById(id);
    }

    var map = new BMap.Map("hd-map");

    var point = new BMap.Point(116.400244, 39.92556);
    map.centerAndZoom(point, 16);

    //设置点
    var marker = new BMap.Marker(point);// 创建标注
    map.addOverlay(marker);             // 将标注添加到地图中
    marker.enableDragging();           // 可拖拽

    marker.addEventListener("mouseup", function (e) {
        var point = marker.getPosition();
        document.getElementById('lng').value = point.lng;
        document.getElementById('lat').value = point.lat;
    });

    var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
        {
            "input": "suggestId"
            , "location": map
        });

    ac.addEventListener("onhighlight", function (e) {  //鼠标放在下拉列表上的事件
        var str = "";
        var _value = e.fromitem.value;
        var value = "";
        if (e.fromitem.index > -1) {
            value = _value.province + _value.city + _value.district + _value.street + _value.business;
        }
        str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

        value = "";
        if (e.toitem.index > -1) {
            _value = e.toitem.value;
            value = _value.province + _value.city + _value.district + _value.street + _value.business;
        }
        str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
        G("searchResultPanel").innerHTML = str;
    });

    var myValue;
    ac.addEventListener("onconfirm", function (e) {    //鼠标点击下拉列表后的事件
        var _value = e.item.value;
        myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
        G("searchResultPanel").innerHTML = "onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;

        setPlace();
    });

    function setPlace() {
        function myFun() {
            var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
            map.centerAndZoom(pp, 18);
            //设置文本框
            document.getElementById('lng').value = pp.lng;
            document.getElementById('lat').value = pp.lat;
            //改变点位置
            marker.setPosition(pp);
        }

        var local = new BMap.LocalSearch(map, { //智能搜索
            onSearchComplete: myFun
        });
        local.search(myValue);
    }
}